//
//  ListenViewController.m
//  Koala_Fm
//
//  Created by dllo on 16/1/16.
//  Copyright © 2016年 dllo. All rights reserved.
//
#import "ListenViewController.h"
#import "ArticleCollectionViewCell.h"
#import <UIImageView+WebCache.h>
#import "AFNetBlock.h"
#import "BarModel.h"// 滑动栏的Model
#import "PageCollectionViewCell.h"
#import "ListendataHandler.h"
#import "ReferViewController.h"
#import "DetaLisViewController.h"
// 详情页
#import "SearchViewController.h"

#import "UMSocialSnsService.h"// 分享
#import "UMSocial.h"
@interface ListenViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, PassDelegate>//详情页协议

@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dateArr;
@property(nonatomic, retain)UICollectionView *collection;
@property(nonatomic, retain)UICollectionView *collectionPage;// 整页滑动
@property(nonatomic, retain)UIScrollView *scroll;
@property(nonatomic, retain)UIView *header;// 头部区域
@property(nonatomic, retain)NSMutableArray *mArr;
@property(nonatomic, retain)NSString *arr;
@end

@implementation ListenViewController
- (void)dealloc
{
    [_tableView release];
    [_dateArr release];
    [_collection release];
    [_collectionPage release];
    [_scroll release];
    [_header release];
    [_mArr release];
    [_arr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.mArr = [ListendataHandler setmArr];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height- 100) style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_tableView];
    [_tableView release];
    /** 导航栏设置 **/
    [self navigationBar];
    /** 数据获取 **/
    [self detaHandler];
    /** 小滑动条 **/
    [self article];
    /** 头部区域 **/
//    [self headers];
    /** 轮播图 **/
//    [self Shuffling];
    /** 整页滑动 **/
    [self fullPage];
}
#pragma mark - 整页翻动
- (void)fullPage{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    // 滚动方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // cell 大小
    layout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height- 30- 64 - 49);
    layout.minimumLineSpacing =0;
    self.collectionPage = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height - 30 - 64 - 49)collectionViewLayout:layout];
    _collectionPage.backgroundColor = [UIColor whiteColor];
    _collectionPage.pagingEnabled = YES;
    _collectionPage.delegate = self;
    _collectionPage.dataSource = self;
    [_collectionPage release];
    [self.view addSubview:_collectionPage];
    // 重用池
    [_collectionPage registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"pageCell"];
}
#pragma mark - 头部区域
- (void)headers{
   self.header = [[UIView alloc]initWithFrame:CGRectMake(0, 30, 0, 200)];
//    _header.backgroundColor = [UIColor blackColor];
    _tableView.tableHeaderView = _header;
}
#pragma mark - 轮播图
- (void)Shuffling{
    _scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
//    _scroll.backgroundColor = [UIColor cyanColor];
    [_header addSubview:_scroll];
    _scroll.contentSize = CGSizeMake(self.view.frame.size.width * 3, 200);
    [_scroll release];
    _scroll.pagingEnabled = YES;

}
#pragma mark - 滑动条数据获取
- (void)detaHandler{
    self.arr = @"http://api.kaolafm.com/api/v4/category/list?fid=114&installid=0002JX3E&udid=bd80d675b1bb04bd76de2a42a3aca09d&sessionid=bd80d675b1bb04bd76de2a42a3aca09d1453080696382&imsi=460077347218243&operator=0&lon=121.545427&lat=38.883467&network=1&timestamp=1453084275&playid=599665c96f0bbe24cc2a9375847032d2&sign=ad0e64ef4e4695f26aab782327753a6b&resolution=720*1280&devicetype=0&channel=B-xiaomi&version=4.5.1&appid=0&";
    [AFNetBlock GetDataWithURL:_arr block:^(id block) {
        NSArray *array = block[@"result"][@"dataList"];
        self.dateArr = [BarModel modelHandlerWithArray:array];
//        NSLog(@"%@", _dateArr);
       
        // 刷新页面
        [_collection reloadData];
        [_tableView reloadData];
        [_collectionPage reloadData];
    
       }];
}
#pragma mark - 滑动条
- (void)article{
   UICollectionViewFlowLayout *layou = [[UICollectionViewFlowLayout alloc]init];
    // 滑动方向
    layou.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    // cell大小
    layou.itemSize = CGSizeMake(60, 30);
    layou.minimumLineSpacing = 1;
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(10, 0,self.view.frame.size.width - 30, 30) collectionViewLayout:layou];
    _collection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_collection];
    _collection.dataSource = self;
    _collection.delegate = self;
    [_collection release];
    // 重用池
    [_collection registerClass:[ArticleCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    /** button **/
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(345, 0, 30, 30);
    button.backgroundColor = [UIColor blueColor];
    [self.view addSubview:button];
    // button 添加文字
    [button setTitle:@"最新" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:10];
}

#pragma mark - 导航栏设置
- (void)navigationBar{
    self.navigationItem.title = @"相声评书";
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    // 设置导航栏为不透明
    self.navigationController.navigationBar.translucent = NO;
    // 左按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(share)];
    
    // 按钮颜色
//    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
}

#pragma mark - 左按钮
//- (void)leftAction{

//}


#pragma mark - collection 设置
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dateArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView ==_collection ) {
        ArticleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
           BarModel *m = _dateArr[indexPath.row];
           cell.lable.text = m.categoryName;
        return cell;
    }
    if(collectionView == _collectionPage){

            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pageCell" forIndexPath:indexPath];
        
            ReferViewController *other = [[ReferViewController alloc]init];
        // 设置详情页协议代理人
             other.delegate = self;
        
            UITableView *tableView = [other tableViewSetWithUrlString:_mArr[indexPath.item]];
            [cell addSubview:tableView];
        
        
            return cell;
    }
    return NULL;
}
// coll 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

   // 点击coll改变偏移量
  _collectionPage.contentOffset = CGPointMake(self.view.frame.size.width *indexPath.item, 0);
    

}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView == _collectionPage) {
        _collection.contentOffset = CGPointMake(scrollView.contentOffset.x / self.view.frame.size.width, scrollView.contentOffset.y);
    }
}



// 协议方法
- (void)pass:(NSString *)string name:(NSString *)name iamge:(NSString *)image{
    DetaLisViewController *dataLis = [[DetaLisViewController alloc]init];
   // 传值到下一页
    dataLis.str = string;
    dataLis.name = name;
    dataLis.image = image;
     //跳转到详情页
    [self.navigationController pushViewController:dataLis animated:YES];
}

// 分享
- (void)share{
    
[UMSocialSnsService presentSnsIconSheetView:self
    appKey:@"56a87665e0f55a79ce0025fd"
    shareText:@"你要分享的文字"
    shareImage:[UIImage imageNamed:@"icon.png"]
    shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,nil]
 
        delegate:self];
    //[UMSocialConfig setSupportedInterfaceOrientations:UIInterfaceOrientationMaskLandscape];
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
